package com.wgj.android.wxapi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wgj.android.entity.Member;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.http.model.AccessToken;
import com.wgj.android.ui.activity.LoginActivity;
import com.wgj.android.ui.activity.MainActivity;
import com.wgj.android.utils.CacheUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * ========================================
 * 作    者： 佳爱网络有限公司(wwww.jiaailove.com)
 * 版    本： 1.0
 * 创建日期：  2017/8/12
 * 描    述：  微信分享页面（能够接收分享结果和分享请求）
 * 修订历吏：
 * =========================================
 */
public class WXEntryActivity extends Activity  implements IWXAPIEventHandler {
    private IWXAPI api;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this,
                CacheUtils.getSystemConfig().getWXAppId(), true);
        api.registerApp( CacheUtils.getSystemConfig().getWXAppId());
        //api.registerApp("wx5c54e79bc8e124a1");
        api.handleIntent(getIntent(), this);

    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent,this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
            String i = "";
    }

    @Override
    public void onResp(BaseResp resp) {
        int result = 0;
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                    if(resp.transaction!=null&&resp.transaction.contains("webpage")){
                        XToast.success("分享成功");
                    }else {
                        bindWX(resp);
                    }
                break;
            default:
                    if(resp.transaction!=null&&resp.transaction.contains("webpage")){
                        XToast.success("分享失败");
                    }
                break;
        }
        this.finish();
    }

    private void bindWX(BaseResp resp) {
        if(((SendAuth.Resp) resp).state.equals("wechat_sdk_demo_test")){
            Map<String,Object> params=new HashMap<String,Object>();
            params.put("mId", CacheUtils.getMember().getM_id());
            String str = ((SendAuth.Resp) resp).code;
            params.put("code", ((SendAuth.Resp) resp).code);
            XHttp.obtain().post(WGJAPI.WXBund_URL, params, new HttpCallBack<String>() {
                @Override
                public void onSuccess(String s) {
                    finish();
                    XToast.success("授权成功");
                }
                @Override
                public void onFailed(int i, String s, boolean b) {
                    finish();
                    XToast.error("授权失败");
                }
            });
        }else if(((SendAuth.Resp) resp).state.equals("wxlogin")){
            EventBus.getDefault().post(resp);
            finish();
        }
    }

}